layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 金额相关设置管理
     */
    var UserMoneySetting = {
        tableId: "userMoneySettingTable"
    };

    /**
     * 初始化表格的列
     */
    UserMoneySetting.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: ''},
            {field: 'frozenCapital', sort: true, title: '冻结金额设置',templet: function (d) {
                    var frozenCapital =d.frozenCapital/100;
                    return frozenCapital;
            }},
            {field: 'minAvailSetting', sort: true, title: '最低可用挂售',templet: function (d) {
                    var minAvailSetting =d.minAvailSetting/100;
                    return minAvailSetting;
                }},
            {field: 'maxAvailSetting', sort: true, title: '最高可用挂售',templet: function (d) {
                    var maxAvailSetting =d.maxAvailSetting/100;
                    return maxAvailSetting;
                }},
            {field: 'minExpandSetting', sort: true, title: '最低返利挂售',templet: function (d) {
                    var minExpandSetting =d.minExpandSetting/100;
                    return minExpandSetting;
                }},
            {field: 'maxExpandSetting', sort: true, title: '最高返利挂售',templet: function (d) {
                    var maxExpandSetting =d.maxExpandSetting/100;
                    return maxExpandSetting;
                }},
            {field: 'minRebateSetting', sort: true, title: '最低推广挂售',templet: function (d) {
                    var minRebateSetting =d.minRebateSetting/100;
                    return minRebateSetting;
                }},
            {field: 'maxRebateSetting', sort: true, title: '最高推广挂售',templet: function (d) {
                    var maxRebateSetting =d.maxRebateSetting/100;
                    return maxRebateSetting;
                }},
            {field: 'frozenRebate', sort: true, title: '冻结返利（%）'},
            {field: 'frozenDay', sort: true, title: '冻结天数'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    UserMoneySetting.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(UserMoneySetting.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    UserMoneySetting.openAddDlg = function () {
        func.open({
            title: '添加金额相关设置',
            content: Feng.ctxPath + '/userMoneySetting/add',
            width :"1100px",
            tableId: UserMoneySetting.tableId
        });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    UserMoneySetting.openEditDlg = function (data) {
        func.open({
            title: '修改金额相关设置',
            content: Feng.ctxPath + '/userMoneySetting/edit?id=' + data.id,
            tableId: UserMoneySetting.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    UserMoneySetting.exportExcel = function () {
        var checkRows = table.checkStatus(UserMoneySetting.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    UserMoneySetting.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/userMoneySetting/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(UserMoneySetting.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + UserMoneySetting.tableId,
        url: Feng.ctxPath + '/userMoneySetting/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: UserMoneySetting.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        UserMoneySetting.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        UserMoneySetting.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        UserMoneySetting.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + UserMoneySetting.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            UserMoneySetting.openEditDlg(data);
        } else if (layEvent === 'delete') {
            UserMoneySetting.onDeleteItem(data);
        }
    });
});
